<template>
	<view class="content">
		<view class="t-goods" v-if="goodsList&&goodsList.length>0">
			<view class="t-goods-item" v-for="(item,index) in goodsList" :key="index">
				<image class="t-goods-img" :src="item.goodsUrl"></image>
				<view class="t-goods-title t-text-oneline">{{item.goodsDesc}}</view>
				<view class="t-goods-score t-flex-row-s">
					<text>{{item.goodsScore}}</text>
					<text>积分</text>
				</view>
				<view class="t-dh t-flex-row" @click="toTips(item.goodsId,item.goodsDesc,item)" hover-class="t-click-class">
					我要兑换
				</view>
			</view>
		</view>
		<view class="t-more t-flex-row">
			我也是有底线的哦~
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				goodsList: [
					// {
					// 	goodsUrl: '/static/goods/8.png',
					// 	goodsDesc: '600ML保温杯一个',
					// 	goodsScore: 12000
					// },
				],
				totalPrice:0,
			}
		},
		mounted() {
			this.getGoodsList()
			this.getMyGoldCoins()
		},
		methods: {
			//查询我的金币余额
			getMyGoldCoins() {
				this.$request('signin.signinGoidConins', {
					sysOrgCode: uni.getStorageSync('sysOrgCode'),
					memberId: uni.getStorageSync('id')
				}).then(res => {
					console.log("查询我的金币余额",res)
					if (res.code == 200) {
						this.totalPrice = res.result.totalPrice
					}
			
				})
			},
			getGoodsList() {
				this.$request('signin.signinGoodsList', {
					sysOrgCode: uni.getStorageSync('sysOrgCode'),
				}).then(res => {
					if (res.code == 200) {
						res.result.forEach(it => {
							const obj = {
								goodsUrl: it.goodsIcon,
								goodsDesc: it.goodsName,
								goodsScore: it.currencyQuantity,
								goodsId: it.id
							}
							this.goodsList.push(obj)
						})
					}

				})
			},
			//兑换
			toTips(id, name,item) {
				const that = this
				uni.showModal({
					title: '兑换',
					content: '是否确定兑换' + name,
					success: function(res) {
						if (res.confirm) {
							that.getGoodsExchange(id,item)
						} else if (res.cancel) {
							console.log('用户点击取消兑换');
						}
					}
				});

			},
			//兑换
			getGoodsExchange(id,item) {
				console.log("查询商品",item)
				this.getMyGoldCoins()
				if(this.totalPrice >= item.goodsScore){
					this.$request('signin.signinGoodsExchange', {
						sysOrgCode: uni.getStorageSync('sysOrgCode'),
						memberId: uni.getStorageSync('id'),
						goodsId: id,
					}).then(res => {
						if (res.code == 200) {
							uni.showToast({
								icon: 'none',
								title: '兑换成功！'
							});
						} else if (res.code == 602) {
							uni.navigateTo({
								url: '/pages/home/bonus/address/address'
							})
						} else {
							uni.showToast({
								icon: 'none',
								title: '兑换失败！'
							});
						}
					})
				}else{
					uni.showToast({
						icon: 'none',
						title: '余额不足！'
					});
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	@import 'mall.scss'
</style>